package com.laikan.reader.core;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @param <T>
 *
 * @Description:适配器封装类
 */
public abstract class LaikanBaseAdapter<T> extends BaseAdapter {

    protected Context mContext;
    protected LayoutInflater mInflater;
    protected List<T> datas;

    public LaikanBaseAdapter() {
    }

    public LaikanBaseAdapter(Context context) {
        mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.datas = null;
    }

    public LaikanBaseAdapter(Context context, List<T> datas) {
        mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.datas = datas;
    }

    public void cleanDatas() {
        if (datas != null && !datas.isEmpty()) {
            datas.clear();
            notifyDataSetChanged();
        }
    }

    public List<T> getDatas() {
        return this.datas;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void addDatas(ArrayList<T> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null != datas) {
            return datas.size();
        }
        return 0;
    }

    @Override
    public T getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract View getView(int position, View convertView, ViewGroup arg2);

}
